package com.simtop.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;
import com.simtop.pojo.u3dpo.ModelData;
import com.simtop.pojo.u3dpo.Momo;
import com.simtop.pojo.u3dpo.Uds;
import com.simtop.pojo.userpo.User;
import com.simtop.pojo.experimentpo.ExperimentDynamicVo;
import com.simtop.service.ExperimentService;
import com.simtop.util.jwtutil.JwtUtil;
import com.simtop.util.redisutil.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: liuzhaobo
 * @Description: U3D交互
 * @DateTime: 2022/8/31 0031 16:55
 * @Params:
 * @Return
 */
@Controller
@CrossOrigin
@RequestMapping("/unity")
public class U3dController {

    @Autowired
    private ExperimentService experimentService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * @Author: liuzhaobo
     * @Description: 记录场景信息 用户唯一id + 唯一时间戳 + 场景类
     * @DateTime: 2022/10/12 0012 11:35
     * @Params:
     * @Return
     */
    @PostMapping("/add")
    @ResponseBody
    public ServerResponse add(@RequestBody ModelData modelData, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            redisUtil.set(u.getId() + ":" + u.getMillTime() + ":" + modelData.getId(), modelData, 10 * 60 * 60 * 1000L);
            return ServerResponse.createBySuccessMsg("提交内容成功");
        } catch (Exception e) {
            return ServerResponse.createByErrorMsg("提交内容失败");
        }
    }

    /**
     * 重置场景信息 用户唯一id + 唯一时间戳 + 场景类
     *
     * @param request
     * @return
     */
    @PostMapping("/refresh")
    @ResponseBody
    public ServerResponse refresh(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            ModelData modelData = new ModelData(1, 0, 0);
            ModelData modelData1 = new ModelData(2, 0, 0);
            redisUtil.set(u.getId() + ":" + u.getMillTime() + ":" + 1, modelData, 10 * 60 * 60 * 1000L);
            redisUtil.set(u.getId() + ":" + u.getMillTime() + ":" + 2, modelData1, 10 * 60 * 60 * 1000L);
            return ServerResponse.createBySuccessMsg("提交内容成功");
        } catch (Exception e) {
            return ServerResponse.createByErrorMsg("提交内容失败");
        }
    }


    /**
     * @Author: liuzhaobo
     * @Description: 获取场景信息
     * @DateTime: 2022/10/12 0012 11:29
     * @Params:
     * @Return
     */
    @PostMapping("/get")
    @ResponseBody
    public ServerResponse get(@RequestBody ModelData modelData, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            ModelData data = (ModelData) redisUtil.get(u.getId() + ":" + u.getMillTime() + ":" + modelData.getId());
            return ServerResponse.createBySuccess("获取内容成功", data);
        } catch (Exception e) {
            return ServerResponse.createByErrorMsg("获取内容失败");
        }
    }

    /**
     * @Author: liuzhaobo
     * @Description: 提交成绩 核心  修正 三合一 判断提交 本地 + ilab + 重庆
     * @DateTime: 2022/10/12 0012 11:31
     * @Params:
     * @Return
     */

    @PostMapping("/addu3d")
    @ResponseBody
    public ServerResponse addu3d(@RequestBody String data, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }

        try {
            int id = u.getId();
            String username = u.getUsername();
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer userTimess = jsonObject.getInteger("userTime");
            int second = userTimess; //这是随便输入的秒值
            int hour = second / 3600; // 得到分钟数
            second = second % 3600;//剩余的秒数
            int minute = second / 60;//得到分
            second = second % 60;//剩余的秒
            String userTime = String.format("%02d:%02d:%02d", hour, minute, second);
            String conmmitTime = jsonObject.getString("ConmmitTime");
            Integer totalScore = jsonObject.getInteger("TotalScore");
            Integer score_read = jsonObject.getInteger("Score_read");
            Integer score_sigin = jsonObject.getInteger("Score_sigin");
            Integer score_begin = jsonObject.getInteger("Score_begin");
            Integer score_zt = jsonObject.getInteger("Score_zt");
            Integer score_jzcf = jsonObject.getInteger("Score_jzcf");
            Integer score_zts = jsonObject.getInteger("Score_zts");
            Integer score_fac = jsonObject.getInteger("Score_fac");
            Integer score_hy = jsonObject.getInteger("Score_hy");
            Integer score_wy = jsonObject.getInteger("Score_wy");
            Integer score_kh = jsonObject.getInteger("Score_kh");
            String qd_summary = jsonObject.getString("qd_Summary");
            String yj_summary = jsonObject.getString("yj_Summary");
            String hy_summary = jsonObject.getString("hy_Summary");
            String wy_summary = jsonObject.getString("wy_Summary");
            experimentService.addu3ddddd(id, username, userTime, conmmitTime, totalScore, score_read, score_sigin, score_begin, score_zt,
                    score_jzcf, score_zts, score_fac, score_hy, score_wy, score_kh, qd_summary, yj_summary, hy_summary, wy_summary);
        } catch (Exception e) {
            return ServerResponse.createByError();
        }

        return ServerResponse.createBySuccessMsg("提交成绩成功");

    }

    /**
     * @Author: liuzhaobo
     * @Description: 查看实验成绩
     * @DateTime: 2022/10/12 0012 11:32
     * @Params:
     * @Return
     */

    @PostMapping("/findExperimental")
    @ResponseBody
    public ServerResponse findExperimental(@RequestBody ExperimentDynamicVo experimentDynamicVo, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        //判断不是管理员和教师身份
        if (!u.getRoleId().equals(1) && !u.getRoleId().equals(2)) {
            PageHelper.startPage(experimentDynamicVo.getPageNum(), experimentDynamicVo.getPageSize());
            //只找自己\
            System.out.println(u.getId());
            List<Uds> allsy2 = experimentService.allsy2(u.getId());
            System.out.println(allsy2);
            PageInfo<Uds> pageInfo = new PageInfo<Uds>(allsy2);
            return ServerResponse.createBySuccess("获取实验报告成功", pageInfo);
        }
        PageHelper.startPage(experimentDynamicVo.getPageNum(), experimentDynamicVo.getPageSize());
        //List<ExperimentReportVo> allReport = experimentService.getAllReport(null,experimentDynamicVo.getMessage());
        //全查
        List<Uds> allallsy02 = experimentService.allallsy02();
        PageInfo<Uds> pageInfo = new PageInfo<Uds>(allallsy02);
        return ServerResponse.createBySuccess("获取实验报告成功", pageInfo);
    }

    /**
     * @Author: liuzhaobo
     * @Description: 删除实验成绩
     * @DateTime: 2022/10/12 0012 11:33
     * @Params:
     * @Return
     */

    @PostMapping("/deleteExperimental")
    @ResponseBody
    public ServerResponse deleteExperimental(@RequestBody String data, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        JSONObject jsonObject = JSON.parseObject(data);
        Integer udsid = jsonObject.getInteger("udsid");
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        //判断不是管理员和教师身份
        if (!u.getRoleId().equals(1) && !u.getRoleId().equals(2)) {

            return ServerResponse.createByErrorMsg("无权限");
        }
        experimentService.deleteExperimental(udsid);
        return ServerResponse.createBySuccess("删除实验报告成功");

    }
}
